<template>
  <el-dialog
    v-model="visible"
    width="1200px"
    :close-on-click-modal="false"
    :before-close="handleClose"
    append-to-body
    v-loading="loading"
    class="unified-dialog edit-dialog-tabs"
  >
    <template #header>
      <DialogHeader
        icon="Edit"
        title="标签页编辑表单示例"
        description="使用 el-tabs 分组，适用于字段较多、需要多个标签页分类的编辑场景"
        type="primary"
      />
    </template>
    
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="140px"
      label-position="left"
    >
      <el-tabs v-model="activeTab" class="config-tabs">
        <!-- Tab 1: 基础信息 -->
        <el-tab-pane label="基础信息" name="basic">
          <div class="tab-content">
            <el-form-item label="Topic名称" prop="topic_name">
              <el-input
                v-model="form.topic_name"
                placeholder="请输入Topic名称"
                :disabled="true"
                maxlength="100"
                show-word-limit
              />
              <span class="form-tip">Topic名称不可修改</span>
            </el-form-item>
            
            <el-form-item label="分区ID" prop="partition_id">
              <el-input-number
                v-model="form.partition_id"
                :min="-1"
                :max="999"
                :disabled="true"
                style="width: 100%"
              />
              <span class="form-tip">分区ID不可修改</span>
            </el-form-item>
            
            <el-form-item label="配置描述" prop="description">
              <el-input
                v-model="form.description"
                type="textarea"
                :rows="2"
                placeholder="请输入配置描述"
                maxlength="500"
                show-word-limit
              />
            </el-form-item>
          </div>
        </el-tab-pane>

        <!-- Tab 2: 监控设置 -->
        <el-tab-pane label="监控设置" name="monitor">
          <div class="tab-content">
            <el-form-item label="启用监控" prop="is_monitored">
              <div class="switch-container">
                <el-switch
                  v-model="form.is_monitored"
                  :active-value="1"
                  :inactive-value="0"
                  size="large"
                />
                <div class="switch-description">
                  <p class="description-title">监控数据收集</p>
                  <p class="description-text">启用后将定时收集Topic的消息数据和Offset信息</p>
                </div>
              </div>
            </el-form-item>
            
            <el-form-item label="启用告警" prop="is_alert_enabled">
              <div class="switch-container">
                <el-switch
                  v-model="form.is_alert_enabled"
                  :active-value="1"
                  :inactive-value="0"
                  size="large"
                />
                <div class="switch-description">
                  <p class="description-title">消息延迟告警</p>
                  <p class="description-text">启用后当消息延迟超过阈值时会发送告警通知</p>
                </div>
              </div>
            </el-form-item>
            
            <el-form-item label="监控间隔(分钟)" prop="monitor_interval">
              <el-input-number
                v-model="form.monitor_interval"
                :min="1"
                :max="60"
                placeholder="分钟"
                style="width: 200px"
              />
              <span class="form-tip">数据收集间隔时间</span>
            </el-form-item>
          </div>
        </el-tab-pane>

        <!-- Tab 3: 告警配置 -->
        <el-tab-pane label="告警配置" name="alert">
          <div class="tab-content">
            <el-form-item label="告警阈值(小时)" prop="alert_threshold_hours">
              <el-input-number
                v-model="form.alert_threshold_hours"
                :min="1"
                :max="168"
                placeholder="请输入告警阈值"
                style="width: 200px"
              />
              <span class="form-tip">小时无新消息则触发告警</span>
            </el-form-item>
            
            <el-form-item label="Webhook URL" prop="webhook_url">
              <el-input
                v-model="form.webhook_url"
                placeholder="请输入企业微信/钉钉Webhook地址"
                clearable
              />
            </el-form-item>
            
            <el-form-item label="首次告警时间" prop="alert_first_send_time">
              <el-time-select
                v-model="form.alert_first_send_time"
                start="00:00"
                step="00:30"
                end="23:30"
                placeholder="选择首次告警发送时间"
                style="width: 200px"
              />
              <span class="form-tip">建议设置在上班时间</span>
            </el-form-item>
            
            <el-form-item label="第2次告警间隔" prop="alert_second_interval">
              <el-input-number
                v-model="form.alert_second_interval"
                :min="60"
                :max="86400"
                :step="3600"
                placeholder="秒"
                style="width: 200px"
              />
              <span class="form-tip">第2次告警与第1次的间隔时间（秒）</span>
            </el-form-item>
            
            <el-form-item label="第3次告警间隔" prop="alert_third_interval">
              <el-input-number
                v-model="form.alert_third_interval"
                :min="60"
                :max="86400"
                :step="3600"
                placeholder="秒"
                style="width: 200px"
              />
              <span class="form-tip">第3次告警与第2次的间隔时间（秒）</span>
            </el-form-item>
            
            <el-form-item label="最大告警次数" prop="max_alert_count">
              <el-input-number
                v-model="form.max_alert_count"
                :min="1"
                :max="10"
                placeholder="次"
                style="width: 200px"
              />
              <span class="form-tip">单周期内最多发送告警次数</span>
            </el-form-item>
          </div>
        </el-tab-pane>

        <!-- Tab 4: 高级设置 -->
        <el-tab-pane label="高级设置" name="advanced">
          <div class="tab-content">
            <el-form-item label="消费组名称" prop="consumer_group">
              <el-input
                v-model="form.consumer_group"
                placeholder="请输入消费组名称（可选）"
                clearable
              />
              <span class="form-tip">用于计算消费滞后，留空则不计算</span>
            </el-form-item>
            
            <el-form-item label="数据保留天数" prop="data_retention_days">
              <el-input-number
                v-model="form.data_retention_days"
                :min="1"
                :max="365"
                placeholder="天"
                style="width: 200px"
              />
              <span class="form-tip">监控数据的保留时间</span>
            </el-form-item>
            
            <el-form-item label="是否启用统计" prop="enable_statistics">
              <el-switch
                v-model="form.enable_statistics"
                :active-value="1"
                :inactive-value="0"
                size="large"
              />
              <span class="form-tip">是否启用数据统计功能</span>
            </el-form-item>
            
            <el-form-item label="备注信息" prop="remarks">
              <el-input
                v-model="form.remarks"
                type="textarea"
                :rows="3"
                placeholder="请输入备注信息"
                maxlength="500"
                show-word-limit
              />
            </el-form-item>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import DialogHeader from '@/components/DialogHeader.vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:modelValue', 'success'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const formRef = ref()
const loading = ref(false)
const activeTab = ref('basic')

const form = ref({
  topic_name: '',
  partition_id: 0,
  description: '',
  is_monitored: 1,
  is_alert_enabled: 1,
  monitor_interval: 5,
  alert_threshold_hours: 24,
  webhook_url: '',
  alert_first_send_time: '09:30',
  alert_second_interval: 7200,
  alert_third_interval: 21600,
  max_alert_count: 3,
  consumer_group: '',
  data_retention_days: 30,
  enable_statistics: 1,
  remarks: ''
})

const rules = {
  alert_threshold_hours: [
    { required: true, message: '请输入告警阈值', trigger: 'blur' },
    { type: 'number', min: 1, max: 168, message: '告警阈值必须在1-168小时之间', trigger: 'blur' }
  ],
  max_alert_count: [
    { required: true, message: '请输入最大告警次数', trigger: 'blur' },
    { type: 'number', min: 1, max: 10, message: '最大告警次数必须在1-10之间', trigger: 'blur' }
  ]
}

const handleClose = () => {
  visible.value = false
  activeTab.value = 'basic'
}

const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    loading.value = true
    
    // 模拟保存
    await new Promise(resolve => setTimeout(resolve, 500))
    
    ElMessage.success('保存成功（演示模式）')
    emit('success')
    handleClose()
  } catch (error) {
    console.error('表单验证失败:', error)
  } finally {
    loading.value = false
  }
}

// 监听对话框显示，初始化表单
watch(visible, (newVal) => {
  if (newVal && props.data) {
    form.value = {
      topic_name: props.data.topic_name || '',
      partition_id: props.data.partition_id ?? 0,
      description: props.data.description || '',
      is_monitored: props.data.is_monitored ?? 1,
      is_alert_enabled: props.data.is_alert_enabled ?? 1,
      monitor_interval: props.data.monitor_interval || 5,
      alert_threshold_hours: props.data.alert_threshold_hours || 24,
      webhook_url: props.data.webhook_url || '',
      alert_first_send_time: props.data.alert_first_send_time || '09:30',
      alert_second_interval: props.data.alert_second_interval || 7200,
      alert_third_interval: props.data.alert_third_interval || 21600,
      max_alert_count: props.data.max_alert_count || 3,
      consumer_group: props.data.consumer_group || '',
      data_retention_days: props.data.data_retention_days || 30,
      enable_statistics: props.data.enable_statistics ?? 1,
      remarks: props.data.remarks || ''
    }
    activeTab.value = 'basic'
  }
})
</script>

<style scoped>
.config-tabs {
  margin-top: 16px;
}

.tab-content {
  padding: 20px;
}

.form-tip {
  margin-left: 12px;
  color: #909399;
  font-size: 12px;
  line-height: 1.4;
}

.switch-container {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.switch-description {
  flex: 1;
}

.description-title {
  margin: 0 0 4px 0;
  color: #303133;
  font-size: 14px;
  font-weight: 500;
}

.description-text {
  margin: 0;
  color: #606266;
  font-size: 12px;
  line-height: 1.4;
}
</style>

